package io.clayton.tmp;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;

public class ImportCss {
	
	private static String baseUri = "http://localhost/";
	private static String baseOutputDir = "C:\\Git\\miep\\miep-weblib\\src\\main\\webapp\\bootstrap\\3.3.6\\themes\\";
	
	private static String cssFile = "bootstrap.css";
	private static String minCssFile = "bootstrap.min.css";
	private static String lessFile = "bootstrap.less";
	private static String minCssFile1 = "bootstrap.min.css";

	public static void main(String[] args) {
		String[] themes = new String[] { "cerulean", "cosmo", "cyborg",
				"darkly", "flatly", "journal", "lumen", "paper", "readable",
				"sandstone", "simplex", "slate", "spacelab", "superhero",
				"united", "yeti" };
	
		for (String theme : themes) {
			download(theme);
		}
	}

	private static void download(String theme) {
		try {
			download(theme, cssFile);
			download(theme, minCssFile);
			System.out.printf("Download %s ok!%n", theme);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private static void download(String theme, String file) throws IOException {
		URL url = new URL(baseUri + theme + "/" + file);
		File dir = new File(baseOutputDir + theme);
		dir.mkdir();
		
		URLConnection c = url.openConnection();
		c.addRequestProperty("Referer", baseUri + theme + "/");
		c.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Maxthon/4.4.8.1000 Chrome/30.0.1599.101 Safari/537.36");
		c.addRequestProperty("Cookie", "__cfduid=d811f294f1b9f5ca050b4629e3efaf0ad1462700254; __utmt=1; __utma=97413516.898016.1462700268.1462700268.1462703348.2; __utmb=97413516.2.10.1462703348; __utmc=97413516; __utmz=97413516.1462700268.1.1.utmcsr=baidu|utmccn=(organic)|utmcmd=organic");
		BufferedInputStream is = new BufferedInputStream(c.getInputStream());
		OutputStream os = new FileOutputStream(new File(dir, file));
		
		byte[] buf = new byte[1024];
		int len = 0;
		while ((len = is.read(buf)) != -1) {
			os.write(buf, 0, len);
		}
		
		is.close();
		os.close();
	}

}
